package com.zzyl.serve.service.impl;

import java.io.IOException;
import java.util.List;

import com.zzyl.common.core.domain.AjaxResult;

import com.zzyl.common.utils.PDFUtil;
import com.zzyl.oss.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.HealthAssessmentMapper;
import com.zzyl.serve.domain.HealthAssessment;
import com.zzyl.serve.service.IHealthAssessmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

import static com.zzyl.common.constant.RedisKeyConstants.HEALTH_ASSESSMENT_KEY;

/**
 * 健康评估Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-04
 */
@Service
public class HealthAssessmentServiceImpl extends ServiceImpl<HealthAssessmentMapper, HealthAssessment> implements IHealthAssessmentService
{
    @Autowired
    private HealthAssessmentMapper healthAssessmentMapper;

    /**
     * 查询健康评估
     *
     * @param id 健康评估主键
     * @return 健康评估
     */
    @Override
    public HealthAssessment selectHealthAssessmentById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询健康评估列表
     *
     * @param healthAssessment 健康评估
     * @return 健康评估
     */
    @Override
    public List<HealthAssessment> selectHealthAssessmentList(HealthAssessment healthAssessment)
    {
        return healthAssessmentMapper.selectHealthAssessmentList(healthAssessment);
    }

    /**
     * 新增健康评估
     *
     * @param healthAssessment 健康评估
     * @return 结果
     */
    @Override
    public int insertHealthAssessment(HealthAssessment healthAssessment)
    {
        return save(healthAssessment) ? 1 : 0;
    }

    /**
     * 修改健康评估
     *
     * @param healthAssessment 健康评估
     * @return 结果
     */
    @Override
    public int updateHealthAssessment(HealthAssessment healthAssessment)
    {
        return updateById(healthAssessment) ? 1 : 0;
    }

    /**
     * 批量删除健康评估
     *
     * @param ids 需要删除的健康评估主键
     * @return 结果
     */
    @Override
    public int deleteHealthAssessmentByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除健康评估信息
     *
     * @param id 健康评估主键
     * @return 结果
     */
    @Override
    public int deleteHealthAssessmentById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 上传体检报告
     *
     * @param idCardNo
     * @param file
     * @return
     */
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public AjaxResult upload(String idCardNo, MultipartFile file) throws IOException {


        try {
            // 1. 上传阿里云oss
            String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
            // 2. PDF内容读取文本，存入Redis
            String text = PDFUtil.pdfToString(file.getInputStream());
            String key = HEALTH_ASSESSMENT_KEY + idCardNo;
            redisTemplate.opsForValue().set(key, text);

            // 3. 返回结果
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("url", url);

            ajaxResult.put("fileName", file.getOriginalFilename());
            ajaxResult.put("originalFilename", file.getOriginalFilename());



            return ajaxResult;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
